package com.zxy.ziems.server.board.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.btp.common.response.JsonResponse;
import com.zxy.ziems.server.alarm.enums.AlarmProcessStatusEnum;
import com.zxy.ziems.server.alarm.mapper.AlarmRecordMapper;
import com.zxy.ziems.server.alarm.pojo.entity.AlarmRecordEntity;
import com.zxy.ziems.server.alarm.pojo.vo.AlarmRecordVO;
import com.zxy.ziems.server.board.constant.BoardConstant;
import com.zxy.ziems.server.board.pojo.vo.DosageVo;
import com.zxy.ziems.server.board.pojo.vo.EnergyTrendVo;
import com.zxy.ziems.server.board.service.EvBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 环保看板
 */
@RestController
@RequestMapping("/api/dataBoard/ev")
public class EVBoardController {

    @Autowired
    private EvBoardService evBoardService;
    @Autowired
    private AlarmRecordMapper alarmRecordMapper;

    /**
     * 实时监测
     * @return
     */
    @GetMapping("/currentValue")
    public JsonResponse<List<DosageVo>> currentValue() {

        return JsonResponse.SUCCESS(evBoardService.currentValue());
    }

    /**
     * 分类趋势
     * @return
     */
    @GetMapping("/classTrend")
    public JsonResponse<EnergyTrendVo> classTrend(@RequestParam("dateType")String dateType) {

        return JsonResponse.SUCCESS(evBoardService.classTrend(dateType));
    }

    /**
     * 告警记录
     * @return
     */
    @GetMapping("/alarmRecord")
    public JsonResponse<List<AlarmRecordVO>> alarmRecord() {

        //获取近7天的报警记录
        QueryWrapper<AlarmRecordEntity> query = new QueryWrapper<>();
        query.lambda().between(AlarmRecordEntity::getAlarmTime, LocalDateTime.now().minusDays(7), LocalDateTime.now())
                .eq(AlarmRecordEntity::getProcessStatus, AlarmProcessStatusEnum.GOING.getCode())
                .eq(AlarmRecordEntity::getDeviceTypeCode, BoardConstant.DeviceTypeCodeEnum.ENVIRONMENT.getTypeCode());

        return JsonResponse.SUCCESS(alarmRecordMapper.selectList(query));
    }
}
